// pages/classRecord/classRecord.js
import api from "../../api/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //#region 设置搜索框
    searchList: [
      {
        name: "shop_id",
        type: "topShop",
        offRefresh: true,
      },
      {
        names: ["time_form", "start_date", "end_date"],
        type: "topDate",
        name: "topDate",
      },
      {
        name: "search",
        type: "input",
        label: "会员姓名、手机号",
      },
      {
        name: "course_name",
        type: "input",
        label: "课程名称",
      },
      {
        name: "coach_id",
        type: "pickerCoach",
        label: "教练",
        span: 10,
      },
      {
        name: "class_action",
        type: "picker",
        label: "行为",
        span: 6,
      },
    ],
    searchNum: [
      {
        label: "课数",
        show: true,
        num: 0,
      },
      {
        label: "节数(节)",
        num: 0,
      },
      {
        label: "上课金额(元)",
        show: true,
        num: "0.00",
      },
    ],
    sExplain: {
      title: "搜索说明",
      content:
        "时间可以选择，对会员名称、手机号、课程名称、上课行为、教练进行搜索",
    },

    nExplain: {
      title: "统计说明",
      content: "根据搜索条件对上课课数、节数、金额进行统计",
    },
    searchParam: {
      time_form: "month",
    },
    //#endregion

    actTab: "class",

    //#region 列表
    list: [],
    isMore: true,
    page: 1,
    isLoading: false,
    //#endregion
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      list: [],
      isMore: true,
      page: 1,
      "searchParam.shop_id": wx.getStorageSync("shopID"),
    });
    this.getDataList();
  },
  searchChonge(e) {
    this.setData({
      searchParam: e.detail,
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  switchTab(e) {
    this.setData({
      actTab: e.detail.name,
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  getDataList() {
    let { page, list, searchParam, searchNum, actTab } = this.data;
    searchParam = {
      ...searchParam,
      view_form: actTab,
    };
    this.setData({
      isLoading: true,
    });
    api
      .apiStoreClassList({
        ...searchParam,
        page,
      })
      .then((res) => {
        if (
          JSON.stringify({
            ...this.data.searchParam,
            view_form: this.data.actTab,
          }) !== JSON.stringify(searchParam)
        )
          return;
        let data = res.data;
        if (page === 1) {
          searchNum[0].num = data.entirety.course_num;
          searchNum[1].num = data.entirety.classes_num;
          searchNum[2].num = Number(data.entirety.cost).toFixed(2);
          this.setData({
            searchNum,
          });
        }
        if (page >= data.all_page) this.setData({ isMore: false });
        if (data.list.length) {
          data.list = data.list.map((item) => {
            item.log_list = item.log_list
              ? item.log_list
              : [
                  {
                    class_time: item.class_time,
                    that: item.that,
                    cost: item.cost,
                    class_action: item.class_action,
                    class_action_str: item.class_action_str,
                    attend_name: item.coach_info.nickname,
                    attend_mobile: item.coach_info.mobile,
                  },
                ];
            item.user_info.tagVal = 0;
            item.user_info.tagStr = "会员";
            item.coach_info.tagVal = 2;
            item.coach_info.tagStr = "教练";
            return item;
          });
          list.push(...data.list);
          this.setData({ list });
        }
      })
      .finally((_) => {
        this.setData({
          isLoading: false,
        });
      });
  },
  switchFold(e) {
    let i = e.currentTarget.dataset.index;
    let key = `list[${i}]`;
    this.setData({
      [key]: {
        ...this.data.list[i],
        isFold: !this.data.list[i].isFold,
      },
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isMore) return;
    if (this.data.isLoading) return;
    let page = this.data.page;
    this.setData({
      page: page + 1,
    });
    this.getDataList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
